//This is an utilitary class to add dragable functionality for a scene element.
// setDraggable overwrites the element's mouse events, with the functions that enable drag-drop
// resetDragable returns the mouse events to the original functions
function Draggable(){
	this.draggedObject = null;
	this.precision = 3;
	// adds drag-drop functionality to the specified shape
	this.setDraggable = function(shape){
		var self = this;
		// save original events and status of the shape
		shape.origMouseDown = shape.mouseDown;
		shape.origMouseUp = shape.mouseUp;
		shape.origMouseMove = shape.mouseMove;
		shape.origIgnoreMouseEvents = shape.ignoreMouseEvents;
		shape.ignoreMouseEvents = false;
		// override mouse events on the draggable shape
		shape.mouseDown = function(pos){
			// start dragging
			self.draggedObject = shape;
			self.draggedObject.scene.canvas.style.cursor="hand";
			self.draggedObject.dragOffsets = {x:pos.x-self.draggedObject.x,y:pos.y-self.draggedObject.y};
			
			self.draggedObject.step = self.precision;
		};
		shape.mouseUp = function(pos){
			// stop dragging
			if(self.draggedObject){
				self.draggedObject.scene.canvas.style.cursor="default";
				self.draggedObject = null;
			}
		};
		shape.mouseMove = function(pos){
			if(self.draggedObject){
				//dragging			
				self.draggedObject.step--;
				if(self.draggedObject.step==0){
					self.draggedObject.move(pos.x-self.draggedObject.dragOffsets.x, pos.y-self.draggedObject.dragOffsets.y);
					self.draggedObject.step = self.precision;
				}				
			}
		};
	};
	
	// removes draggable functionality of the specified shape
	this.resetDraggable = function(shape){
		//restores the original events and attributes to the shape
		shape.mouseDown = shape.origMouseDown;
		shape.mouseUp = shape.origMouseUp;
		shape.mouseMove = shape.origMouseMove;
		shape.ignoreMouseEvents = shape.origIgnoreMouseEvents;
	}
}